/*
// 引入 request 文件
import request from './index.js'
 
// 分页查询学习列表
export const pageStudyInfo = (params) => {
	return request({
		url: '/study/studyInfo/page',
		method: 'get',
		data: params,
        header: {} // 自定义'
	})
}
// 获取学习列表详细信息
export const studyInfoById = (id) => {
	return request({
		url: `/study/studyInfo/${id}`,
		method: 'get',
	})
}
*/

import request from "./index.js";
import {
	useUserStore
} from '../stores/user.js'

export const getCampus = () => {
	return request({
		url: '/campuses',
		method: 'get'
	})
}

export const getCanteenList = (data) => {
	return request({
		url: '/getcanteen',
		method: 'post',
		data: {
			campus_id: data
		}
	})
}

export const getResturantList = (data) => {
	return request({
		url: '/getResturant',
		method: 'post',
		data: {
			canteen_id: data
		}
	})
}

export const getResDetail = (uid) => {
	return request({
		url: `/getresdetail`,
		method: 'post',
		data: {
			restaurant_id: Number(uid)
		}
	})
}

export const Authlogin = async (loginMessage) => {
	const res = await request({
		url: `/auth/login`,
		method: 'post',
		data: loginMessage
	});
	if (res.message === 'Login successful') {
		uni.setStorageSync('token', res.token);
		useUserStore().initUserInfo(res.userInfo)
		return {
			success: true,
			role: res.userInfo.role
		};
	} else return {
		success: false
	};
}

export const Authregister = (registerMessage) => {
	return request({
		url: `/auth/register`,
		method: 'post',
		data: registerMessage
	})
}

export const Autologin = async (token) => {
	try {
		const res = await request({
			url: `/auth/auto-login`,
			method: 'post',
			header: {
				'Authorization': `Bearer ${token}`
			},
			data: {}
		});

		if (res.message === "Auto login successful") {
			// Token 验证成功，返回成功结果
			useUserStore().initUserInfo(res.userInfo)
			return {
				success: true,
				token: res.token,
				role: res.userInfo.role
			};
		} else {
			// Token 验证失败，返回失败结果
			return {
				success: false
			};
		}
	} catch (err) {
		// 处理请求过程中的错误
		console.error('自动登录失败:', err);
		return {
			success: false,
			message: '请求发生错误，请检查网络或稍后重试'
		};
	}
};

export const pay = (amount) => {
	return request({
		url: '/pay/deduct-balance',
		method: 'post',
		data: {
			amount
		}
	});
};

export const submitOrder = (param) => {
	return request({
		url: '/order/submit-order',
		method: 'post',
		data: param
	})
}

export const charge = (param) => {
	return request({
		url: '/pay/recharge',
		method: 'post',
		data: param
	})
}

export const queryOrder = (status) => {
	return request({
		url: '/order/query-orders',
		method: 'get',
		data: {
			status
		}
	})
}

export const adminRestaurantItems = (restaurantId) => {
	return request({
		url: '/admin/restaurant-items',
		method: 'get',
		data: {
			restaurantId
		}
	})
}

export const editAdminRestaurantItem = (item, restaurantId) => {
	console.log(item, 'param')
	const param = {
		restaurantId: Number(restaurantId),
		itemId: item.itemId,
		itemName: item.name,
		itemPrice: item.price,
		itemImageUrl: item.imageUrl,
		// itemCategory: item.itemId
	}
	return request({
		url: '/admin/update-restaurant-item',
		method: 'post',
		data: {
			...param
		}
	})
}

export const addAdminRestaurantItem = (item, restaurantId) => {
	console.log(item, 'param')
	const param = {
		restaurantId: Number(restaurantId),
		itemName: item.name,
		itemPrice: Number(item.price),
		itemImageUrl: item.imageUrl,
		itemCategory: "甜品"
	}
	return request({
		url: '/admin/add-restaurant-item',
		method: 'post',
		data: {
			...param
		}
	})
}

export const deleteAdminRestaurantItem = (itemId) => {
	console.log(itemId, 'itemId')
	return request({
		url: '/admin/delete-item',
		method: 'post',
		data: {
			itemId
		}
	})
}

export const changeItemStatus = (state, itemId, restaurantId) => {
	return request({
		url: '/admin/change-item-status',
		method: 'post',
		data: {
			restaurantId,
			itemId,
			state
		}
	})
}

export const adminQueryOrderList = (restaurantId) => {
	return request({
		url: '/admin/order-list',
		method: 'get',
		data: {
			restaurantId
		}
	})
}

export const adminUpdateOrderState = (orderId, newStatus) => {
	return request({
		url: '/order/update-status',
		method: 'post',
		data: {
			orderId,
			newStatus
		}
	})
}

export const queryActivities = (restaurantId) => {
	return request({
		url: '/activity',
		method: 'get',
		data: {
			restaurantId
		}
	})
}

export const queryResturantDetail = (restaurantId) => {
	return request({
		url: '/admin/restaurant-detail',
		method: 'get',
		data: {
			restaurantId
		}
	})
}

export const reviewItem = (data) => {
	const {
		orderId,
		itemId,
		rating,
		comment,
		imageUrl
	} = data
	return request({
		url: '/review/review-item',
		method: 'post',
		data: {
			orderId,
			itemId,
			rating,
			comment,
			imageUrl
		}
	})
}

export const queryFoodDetail = (itemId) => {
	return request({
		url: '/food/detail',
		method: 'get',
		data: {
			itemId
		}
	})
}

export const getRecommend = (campus_id, user_id) => {
	return request({
		url: '/recommend',
		method: 'get',
		data: {
			campus_id,
			user_id
		}
	})
}

export const submitFeedback = (data) => {
	const {
		campusId,
		canteenId,
		restaurantId,
		content
	} = data
	return request({
		url: '/feedback/submit-feedback',
		method: 'post',
		data: {
			campusId,
			canteenId,
			restaurantId,
			content
		}
	})
}

export const queryUserFeedback = () => {
	return request({
		url: '/feedback/user-feedbacks',
		method: 'get'
	})
}

export const queryAdminFeedback = (restaurantId) => {
	return request({
		url: '/feedback/admin-feedbacks',
		method: 'get',
		data: {
			restaurantId
		}
	})
}

export const getwxloginMessage = (code) => {
	return request({
		url: '/wx/get-wx-openid',
		method: 'post',
		data: {
			code
		}
	})
}

export const changePassword = (oldPassword, newPassword) => {
	return request({
		url: '/auth/change-password',
		method: 'post',
		data: {
			oldPassword,
			newPassword
		}
	})
}

export const queryInformation = (param) => {
	return request({
		url: '/auth/update-profile',
		method: 'post',
		data: {
			...param
		}
	})
}

export const addActivity = (param) => {
	return request({
		url: '/create-activity',
		method: 'post',
		data: {
			...param
		}
	})
}

export const queryMerchant = (restaurantId) => {
	return request({
		url: '/merchant',
		method: 'get',
		data: {
			restaurantId
		}
	})
}